package com.example.alpha.campusbase.Fragment;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.dexafree.materialList.card.Card;
import com.dexafree.materialList.card.CardProvider;
import com.dexafree.materialList.card.OnActionClickListener;
import com.dexafree.materialList.card.action.TextViewAction;
import com.dexafree.materialList.card.action.WelcomeButtonAction;
import com.dexafree.materialList.card.provider.ListCardProvider;
import com.dexafree.materialList.listeners.RecyclerItemClickListener;
import com.dexafree.materialList.view.MaterialListView;
import com.example.alpha.campusbase.Activity.MainActivity;
import com.example.alpha.campusbase.R;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator;

/**
 * Created by Alpha on 2016/6/2.
 */
public class MessageFragment extends Fragment implements RecyclerItemClickListener.OnItemClickListener{

    private MaterialListView mListView;
    private ImageView mEmptyImage;
    private Context mContext;
    private FragmentManager fragmentManager;
    public String lastMessage;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fragmentManager=getActivity().getSupportFragmentManager();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View messageView=inflater.inflate(R.layout.message_layout,container,false);
        mContext=getContext();

        mListView= (MaterialListView) messageView.findViewById(R.id.message_card_list);
        mEmptyImage= (ImageView) messageView.findViewById(R.id.message_empty_image);
        mListView.addOnItemTouchListener(this);

        mListView.setItemAnimator(new SlideInLeftAnimator());//绑定左划动画
        mListView.getItemAnimator().setAddDuration(300);//添加动画持续时间
        mListView.getItemAnimator().setRemoveDuration(300);//移除动画持续时间

        //若listview为空则显示默认图片
        mEmptyImage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        mListView.setEmptyView(mEmptyImage);

        // TODO: 2016/6/4 网络加载图片，需要自定义或删除
        Picasso.with(getContext())
                .load("https://www.skyverge.com/wp-content/uploads/2012/05/github-logo.png")
                .resize(100,100)
                .centerInside()
                .into(mEmptyImage);

        //填充listview
        fillArray();

        //为列表项绑定移除操作反馈
        // TODO: 2016/6/10


        return messageView;
    }

    @Override
    public void onItemClick(@NonNull Card card, int position) {
        String title=card.getProvider().getTitle();
        lastMessage=card.getProvider().getDescription();
        Drawable header=card.getProvider().getDrawable();
        ChatFragment chatFragment=new ChatFragment();
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        transaction.addToBackStack(null);
        transaction.replace(R.id.content_frame,chatFragment).commit();
        MainActivity mainActivity= (MainActivity) getActivity();
        mainActivity.onToolbarchanged(false,title);
    }

    @Override
    public void onItemLongClick(@NonNull Card card, int position) {

    }

    // TODO: 2016/6/4 最终需要按照加载数量做出控制,如每次加载多少个卡片,以及卡片在数据列表的位置
    //填充卡片列表
    public void fillArray(){
        List<Card> cards=new ArrayList<>();
        for (int i=0;i<100;i++){
            cards.add(getCard(i));
        }
        mListView.getAdapter().addAll(cards);
    }

    private Card getCard(final int position){

        //预先填充联系人数据
        final CardProvider provider = new Card.Builder(mContext)
                .setTag("BASIC_IMAGE_BUTTON_CARD")
                .setDismissible()//左右划动删除
                .withProvider(new CardProvider<>())
                .setLayout(R.layout.message_card_layout)
                .setTitleGravity(Gravity.START)
                .setSubtitleGravity(Gravity.START)
                .setDrawableConfiguration(new CardProvider.OnImageConfigListener() {
                    @Override
                    public void onImageConfigure(@NonNull RequestCreator requestCreator) {
                        requestCreator.fit();
                    }
                });

        // TODO: 2016/6/10 改为具体的动态更新列表方法
        switch (position%7){
            case 1:
                provider.setDrawable(R.drawable.m01);
                provider.setTitle("李莎莎");
                //不知道为什么，setsubtitle总是不显示，改用setDescription
                provider.setDescription("明天陪我去逛街吧");
                break;
            case 2:
                provider.setDrawable(R.drawable.m02);
                provider.setTitle("张静初");
                provider.setDescription("还记得一年前的那个晚上么");
                break;
            case 3:
                provider.setDrawable(R.drawable.m03);
                provider.setTitle("王美茹");
                provider.setDescription("欠我的饭什么时候还？");
                break;
            case 4:
                provider.setDrawable(R.drawable.m04);
                provider.setTitle("段小雪");
                provider.setDescription("我有几个题不会，你什么时候有空？");
                break;
            case 5:
                provider.setDrawable(R.drawable.m05);
                provider.setTitle("马爽");
                provider.setDescription("晚上跑步去呗？");
                break;
            case 6:
                provider.setDrawable(R.drawable.m06);
                provider.setTitle("赵薇");
                provider.setDescription("明天来我办公室");
                break;
            case 0:
                provider.setDrawable(R.drawable.m07);
                provider.setTitle("范冰冰");
                provider.setDescription("明天去财务把工资领了");
                break;
        }

        return provider.endConfig().build();
    }


}
